<?php 
if(KT!='KhiThe') exit;
class news_model extends khithe_mysql{
	
	public function getIDSk($SeoKey)
	{
		$SeoKey = parent::checkStrMySQL($SeoKey);
		$select = "SELECT ID FROM ".categorys." WHERE SeoKey = '".$SeoKey."'";
		$query = parent::SelectMySQL($select);
		$row = mysql_fetch_assoc($query);
		return $row['ID'];
	}
	public function getSkID($ID)
	{
		$ID = parent::checkStrMySQL($ID);
		$select = "SELECT SeoKey FROM ".categorys." WHERE ID = '".$ID."'";
		$query = parent::SelectMySQL($select);
		$row = mysql_fetch_assoc($query);
		
		return $row['SeoKey'];
	}
	public function getMenunews($ParentID)
	{
		$ParentID = parent::checkStrMySQL($ParentID);
		$select = "SELECT * FROM ".categorys." WHERE ParentID = '". $ParentID ."' ORDER BY `Order` ASC";
		$row = parent::SelectMySQL($select);
		return $row;
	}
	
	public function getSqlcomments($ID)
	{
		$ID = parent::checkStrMySQL($ID);	 
		
		$select = "SELECT * FROM ".comments." WHERE NewsID ='".$ID."' ";
		parent::CloseConnection();
		return parent::SelectMySQL($select);
	}
	
	public function getSqlnews($ParentID,$page,$limit)
	{			
		$ParentID = parent::checkStrMySQL($ParentID);
		
		$strPID = " CategoryID='".$ParentID."'";
		$resultMenu = $this->getMenunews($ParentID);
		while( $rowMenu = mysql_fetch_assoc($resultMenu) )
		{
			$strPID = $strPID." OR CategoryID='".$rowMenu['ID']."' ";		
		}
		
		$strPID = " WHERE ( ".$strPID." ) AND Status = 'true' ";
		
		$str = $ParentID==""?" WHERE Status = 'true' ":$strPID;
		if($limit!=''){
			$page=($page==''?1:$page);
			$start = ($page - 1)* $limit; 
			$strlimit = " LIMIT $start, $limit";
		}
		else{
			$strlimit ='';
		}
				
		$select = "SELECT * FROM ".news." ".$str." ORDER BY ID DESC ".$strlimit." ";
		
		parent::CloseConnection();
		return parent::SelectMySQL($select);
	}	
	
	public function getMenu($ID)
	{
		$ID = parent::checkStrMySQL($ID);
		$select = "SELECT * FROM ".categorys." WHERE ID = '".$ID."'";
		$query = parent::SelectMySQL($select);
		$row = mysql_fetch_assoc($query);
		
		return $row;
	}
	
	public function selectnew($SeoKey,$ID)
	{
		$ID = parent::checkStrMySQL($ID);
		if(!empty($ID)){
			$SeoKey = parent::checkStrMySQL($SeoKey);
			$str = " WHERE  ID = '".$ID."' ";
		}
		else{
			$str = " WHERE  SeoKey = '".$SeoKey."' ";
		}
		$select = "SELECT * FROM ".news." ".$str." ";
		$query = parent::SelectMySQL($select);
		$row = mysql_fetch_assoc($query);
		parent::CloseConnection();
		
		return $row;
	}
	
	public function setComments($arr,$ID)
	{
        
		$Fullname= $_SESSION['Fullname']!=''?$_SESSION['Fullname']:parent::checkStrMySQL($arr['Fullname']);
		$strY = parent::checkStrMySQL($arr['YahooMail']);
		$arrY = explode('@',$strY);
		$YahooChat= $_SESSION['YahooMe']!=''?$_SESSION['YahooMe']:$arrY[0];
		$Content= strip_tags (parent::checkStrMySQL($arr['contents']));
		$userID = ($_SESSION['ID']!=''?$_SESSION['ID']:0);
		
		$update = "INSERT INTO ".comments."(UserID,NewsID, Fullname,YahooChat,Content,PostDate,`Status`) 
									VALUES('".$userID."','".trim($ID)."','".$Fullname."','".$YahooChat."','".$Content."','".time()."','true')";
	
		$_SESSION['Fullname'] = $Fullname;
		$_SESSION['YahooMe'] = $YahooChat;
		
		parent::CloseConnection();
		return parent::UpdateInsertMySQL($update);
		
		
	}
	
	public function set_view($int,$ID)
	{				
		$int = intval($int) + 1;
		$query = " UPDATE ".news." SET `View`='".$int."' WHERE `ID`='".$ID."' ";
		$note = parent::UpdateInsertMySQL($query);	
		parent::CloseConnection();
		return $note;
	}
}
?>